package com.tripcube.file.service.impl;

import com.tripcube.common.core.utils.file.FileTypeUtils;
import com.tripcube.common.security.utils.SecurityUtils;
import com.tripcube.file.mapper.SysFileMapper;
import com.tripcube.file.service.ISysFileInfoService;
import com.tripcube.file.service.ISysFileService;
import com.tripcube.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 文件信息Service实现类
 */
@Service
public class SysFileInfoServiceImpl implements ISysFileInfoService {

    @Autowired
    private SysFileMapper sysFileMapper;

    @Autowired
    private ISysFileService fileStorageService;

    @Override
    public List<SysFile> selectSysFileList(SysFile sysFile,Integer pageNum, Integer pageSize) {
        return sysFileMapper.selectSysFileList(sysFile);
    }

    @Override
    public SysFile selectSysFileById(Long fileId) {
        return sysFileMapper.selectSysFileById(fileId);
    }

    @Override
    public SysFile selectSysFileByFileName(String fileName) {
        return sysFileMapper.selectSysFileByFileName(fileName);
    }

    @Override
    public int insertSysFile(SysFile sysFile) {
        return sysFileMapper.insertSysFile(sysFile);
    }

    @Override
    public int updateSysFile(SysFile sysFile) {
        sysFile.setUpdateBy(SecurityUtils.getUserId());
        return sysFileMapper.updateSysFile(sysFile);
    }

    @Override
    public int deleteSysFileById(Long fileId) {
        return sysFileMapper.deleteSysFileById(fileId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysFile uploadFileAndSave(MultipartFile file) throws Exception {

        String originalFilename = file.getOriginalFilename();

        // 上传文件
        String fileUrl = fileStorageService.uploadFile(file);

        // 创建文件信息记录
        SysFile sysFile = new SysFile();
        sysFile.setFileName(originalFilename);
        sysFile.setUrl(fileUrl);
        sysFile.setFileSize(file.getSize());
        sysFile.setFileType(file.getContentType());
        sysFile.setCreateTime(new Date());
        sysFile.setUpdateTime(new Date());
        sysFile.setCreateBy(SecurityUtils.getUserId());
        sysFile.setStatus(1);

        // 保存文件信息到数据库
        sysFileMapper.insertSysFile(sysFile);

        return sysFile;
    }

    public List<SysFile> uploadFilesAndSave(MultipartFile[] files) throws Exception {
    List<SysFile> sysFileList = new ArrayList<>();
    List<String> failedFileNames = new ArrayList<>();

    for (MultipartFile file : files) {
        try {
            SysFile sysFile = uploadFileAndSave(file);
            sysFileList.add(sysFile);
        } catch (Exception e) {
            failedFileNames.add(file.getOriginalFilename());
        }
    }

    if (!failedFileNames.isEmpty()) {
        String failedFiles = String.join(", ", failedFileNames);
        if (sysFileList.isEmpty()) {
            throw new Exception("所有文件上传失败");
        } else {
            throw new Exception("部分文件上传失败，失败文件: " + failedFiles);
        }
    }

    return sysFileList;
}



    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFileAndRecord(Long fileId) throws Exception {
        // 查询文件信息
        SysFile sysFile = sysFileMapper.selectSysFileById(fileId);
        if (sysFile == null) {
            throw new Exception("文件不存在");
        }

        // 删除存储中的文件
        fileStorageService.deleteFile(sysFile.getUrl());

        // 删除数据库记录
        sysFileMapper.deleteSysFileById(fileId);

        return true;
    }
}
